#
# This file loads the board configurations from "hardware/arduino/boards.txt"
#

set( ARDUINO_QUIET_BOARDS true)


# load the strings from "hardware/arduino/boards.txt"
file(STRINGS ${ARDUINO_BOARDS_FILE} ARDUINO_BOARDS_STRINGS)
# for each string...
foreach(abs ${ARDUINO_BOARDS_STRINGS})
  # strip whitespace and comments
  string(STRIP ${abs} abs_strip)
  string(REGEX REPLACE "#.*" "" abs_strip "${abs_strip}")
  # test for an equal string
  string(REGEX MATCH "=" eq_test "${abs_strip}")
  if( eq_test )
    # get name as value to the left of =, replacing '.' w/ '_' and upcasing
    string(REGEX MATCH "^[^=]+" name ${abs_strip})
    string(REPLACE "." "_" name ${name})
    string(TOUPPER "${name}" name)
    # get the value
    string(REGEX MATCH "[^=]+$" value ${abs_strip})

    # workaround issue:
    # stk500 is actually stk500v1, so find and fix it:
    if( "${value}" STREQUAL "stk500" )
      set(value "stk500v1")
    endif()

    # set the name/value pair
    set(ARDUINO_${name} "${value}")
    #Is this the name line of a given board, add it to the list of boards!
    string(REGEX MATCH "_NAME" is_name ${name})
    if( is_name )
      string(REPLACE "_NAME" "" platform_name ${name})
      string(TOLOWER  "${platform_name}" platform_name)
#      message( "${platform_name} : ${value}")
      set(ARDUINO_SUPPORTED_PLATFORMS ${ARDUINO_SUPPORTED_PLATFORMS} ${platform_name})
    endif()
  endif()
endforeach()

# now sort the list of boards
list(SORT ARDUINO_SUPPORTED_PLATFORMS)

function( test_arduino_configuration configItem_ labelName_)
  if( NOT ${ARDUINO_QUIET_BOARDS} )
    if( NOT ${configItem_} )
      if( NOT ${labelName_} STREQUAL "atmega8.bootloader.extended_fuses")
        message(WARNING "missing ${labelName_} in ${ARDUINO_BOARDS_FILE}")
      endif()
    endif()
  endif()
endfunction()

# sanity check the list of supported platforms, warning and removing incomplete platforms
foreach(platform ${ARDUINO_SUPPORTED_PLATFORMS})
  string(TOUPPER "${platform}" platform_upper)
  # if any of the parameters are missing let the developer know and tell exactly what is missing!
  test_arduino_configuration( ARDUINO_${platform_upper}_NAME ${platform}.name)
  test_arduino_configuration( ARDUINO_${platform_upper}_UPLOAD_PROTOCOL ${platform}.upload.protocol)
  test_arduino_configuration( ARDUINO_${platform_upper}_UPLOAD_MAXIMUM_SIZE ${platform}.upload.maximum_size )
  test_arduino_configuration( ARDUINO_${platform_upper}_UPLOAD_SPEED ${platform}.upload.speed )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_LOW_FUSES ${platform}.bootloader.low_fuses )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_HIGH_FUSES ${platform}.bootloader.high_fuses )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_EXTENDED_FUSES ${platform}.bootloader.extended_fuses)
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_PATH ${platform}.bootloader.path )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_FILE ${platform}.bootloader.file )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_UNLOCK_BITS ${platform}.bootloader.unlock_bits )
  test_arduino_configuration( ARDUINO_${platform_upper}_BOOTLOADER_LOCK_BITS ${platform}.bootloader.lock_bits )
  test_arduino_configuration( ARDUINO_${platform_upper}_BUILD_MCU ${platform}.build.mcu )
  test_arduino_configuration( ARDUINO_${platform_upper}_BUILD_F_CPU ${platform}.build.f_cpu )
  test_arduino_configuration( ARDUINO_${platform_upper}_BUILD_CORE ${platform}.build.core )
endforeach()


# this is a function to dump the Supported Platform list
function(show_arduino_platforms)
  message( "Supported platforms:")
  foreach(platform ${ARDUINO_SUPPORTED_PLATFORMS})
    string(TOUPPER "${platform}" platform_upper)
    message( "\t${platform} : ${ARDUINO_${platform_upper}_NAME}")
  endforeach()
endfunction()
